package com.sk.weichat.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.adapter.MessageLogin;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.tool.WebViewActivity;
import com.sk.weichat.util.EventBusHelper;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 显示各项html协议的Activity
 */
public class WebAgreementActivity extends BaseActivity {

    private WebView mWebView;
    private String mUrl;
    TextView tvTitle;
    public WebAgreementActivity() {
        noLoginRequired();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_web_view);
        String title = getIntent().getStringExtra("title");
        tvTitle = findViewById(R.id.tv_title_center);
        if(!TextUtils.isEmpty(title)){
            tvTitle.setText(title);
        }
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mUrl = getIntent().getStringExtra("url");
        initView();
        EventBusHelper.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(MessageLogin message) {
        finish();
    }

    private void initView() {
        mWebView = findViewById(R.id.mWebView);
        /* 设置支持Js */
        mWebView.getSettings().setJavaScriptEnabled(true);
        /* 设置为true表示支持使用js打开新的窗口 */
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        /* 设置缓存模式 */
//        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        mWebView.getSettings().setDomStorageEnabled(true);
        /* 设置为使用webView推荐的窗口 */
        mWebView.getSettings().setUseWideViewPort(true);
        /* 设置为使用屏幕自适配 */
        mWebView.getSettings().setLoadWithOverviewMode(true);
        /* 设置是否允许webView使用缩放的功能,我这里设为false,不允许 */
        mWebView.getSettings().setBuiltInZoomControls(false);
        /* 提高网页渲染的优先级 */
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        /* HTML5的地理位置服务,设置为true,启用地理定位 */
        mWebView.getSettings().setGeolocationEnabled(true);
        // 是否允许通过file url加载的Javascript读取本地文件，默认值 false
        mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
        //是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https)，默认值 false
        mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        /* 设置可以访问文件 */
//        mWebView.getSettings().setAllowFileAccess(true);
        // 服务器要求标识 这是imApp内的浏览器
        Map<String, String> head = new HashMap<>();
        head.put("user-agent", "app-shikuimapp");
        mWebView.loadUrl(mUrl, head);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                DialogHelper.showMessageProgressDialog(mContext, "正在获取内容...");
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                DialogHelper.dismissProgressDialog();
                tvTitle.setText(view.getTitle());
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                DialogHelper.dismissProgressDialog();
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
    }

}
